<?php
namespace app\admin\controller\xiservice;


use app\common\controller\Backend;
use app\common\model\xiservice\User;
use app\common\model\xiservice\Order;
use app\common\model\xiservice\Dividend;
use app\common\model\xiservice\Service;
use fast\Date;
use think\Db;

class Console extends Backend
{
    protected $noNeedRight = ['*'];

    /**
     * 查看
     */
    public function index()
    {
        $todayUserCount = User::whereTime('createtime','today')
                            ->count('distinct user_id');
        $allUserCount = User::count('distinct user_id');
        $todayIncomeMoney = Order::where('order_status','in','40,50')
                                ->whereTime('createtime','today')
                                ->sum('pay_money');
        $allIncomeMoney =  Order::where('order_status','in','40,50')
                            ->sum('pay_money');


        $this->view->assign('today_user_count', $todayUserCount);
        $this->view->assign('all_user_count',$allUserCount);
        $this->view->assign('today_income_money', $todayIncomeMoney);
        $this->view->assign('all_income_money', $allIncomeMoney);

        #统计图
        $format = '%Y-%m-%d';

        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        //用户新增趋势-最近七天
        $userlist = User::field("COUNT(user_id) as num,from_unixtime(createtime, '{$format}') AS dates")->where('createtime','between',[$starttime,$endtime])->group('dates')->select();
        $diff_day = ceil(($endtime-$starttime)/86400);
        $userlist_count = [];
        $array = $userlist ? array_column(collection($userlist)->toArray(),'num','dates') : [];

        #订单收入
        $orderlist = Order::field("SUM(pay_money) as num,from_unixtime(createtime, '{$format}') AS dates")->where('order_status','in','40,50')->where('createtime','between',[$starttime,$endtime])->group('dates')->select();
        $orderlist_count = [];
        $array2 = $orderlist ? array_column(collection($orderlist)->toArray(),'num','dates') : [];


        for ($i=0;$i<$diff_day;$i++){
            $date = date('Y-m-d',$starttime+$i*86400);
            $userlist_count[$date] =   isset($array[$date]) ? $array[$date] :0;
            $orderlist_count[$date] =  isset($array2[$date]) ? $array2[$date] :0;
        }


        $this->view->assign([
            'userlist'    => $userlist_count,
            'orderlist'   => $orderlist_count,
        ]);


        //业务员佣金统计
        $salesman_commission_total = (new Dividend())->sum('divide_money');
        $this->view->assign('salesman_commission_total', $salesman_commission_total);

        //订单成功总量
        $all_order_count = Order::whereIn('order_status', ['50','70'])->count();
        $this->view->assign('all_order_count', $all_order_count);

        // -- 各分类的订单统计
        $services = Service::field('id,name')->where('status', '1')->select();
        $serviceCount = [];
        foreach($services as $k => $v) {
            $serviceCount[$v['name']] = Order::where('service_id', $v['id'])
                ->whereIn('order_status', ['50','70'])
                ->count();
        }
        $this->view->assign('serviceCount', $serviceCount);

        return $this->view->fetch();
    }

}